package spms.front.controller;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import spms.front.config.Invoker;
import spms.front.util.Ajax;

@Controller
public class IconController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	String serverName = "SPMS-DATA";

	@GetMapping("/iconList")
	public Object getIconList(@RequestParam(defaultValue = "0") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize, Model model){
		logger.info("get请求/iconList");
		iconList(pageNum, pageSize, model);
		return "iconList";
	}
	
	@PostMapping("/iconList")
	public Object iconList(@RequestParam(defaultValue = "0") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize, Model model){
		logger.info("post请求/iconList");

		MultiValueMap mapParam = new LinkedMultiValueMap();
		mapParam.add("pageNum",pageNum);
		mapParam.add("pageSize",pageSize);

		JSONObject page = (JSONObject) Invoker.INSTANCE.invoke(serverName,"/icon/findPage",mapParam,JSONObject.class);

		model.addAttribute("page", page);
		return "iconList::iconList";
	}
	
	@ResponseBody
	@PostMapping("/addIcon")
	public Ajax addIcon(Model model,MultiValueMap icon) {
		try {
			Invoker.INSTANCE.invoke(serverName,"/icon/findPage",icon,JSONObject.class);
		}catch(Exception e) {
			logger.error("新增icon失败",e);
		}
		return new Ajax();
	}
}
